//#include "getline.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/** Extern int getline(FILE *fp, char *buf, size_t size);
 * read the next `line` from file fp and stores it as a null-terminated string
 * in buff[0..size-1] and retrns the length of the line (not counting the termi-
 * nating null character). When getword reaches the end of file without consumi-
 * ng a line, it return EOF. If a line is longer then size-1 characters, the ex-
 * cess characters are ignored.
 * It is a checked runtime error for fp or buff to be NULL or for size to be 0.
 */
int getline(FILE *fp, char *buf, size_t size)
{
    //fprintf(stdout, "...getline()\n");
    char c = 'a';
    int l_tmp;
    if ( fgets (buf , size , fp) != NULL )
    {
        //fprintf(stdout, "buf = %s\n", buf);
        if((strlen(buf) == size-1) && (buf[size-1] != '\n'))
        {
        do
        {
           c = fgetc(fp);
        }
        while (c != '\n');
        }
        l_tmp = strlen(buf);
        buf[l_tmp-1]='\0';
        return strlen(buf);
    } else
    {
        buf[0] = '\0';
        return EOF;
    }
}
